package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import Models.InvColumn;

public class InvColumnPresenter extends AbstractPresenter<InvColumn> {
	final String SINGLEITEM = "invColumn";
	final String MULTIITEM = "invColumns";
	
	public ArrayList<InvColumn> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public InvColumn tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public InvColumn createItemFromJSONObject(JSONObject jsonObject){
		InvColumn item = new InvColumn();
		try{
			if(!jsonObject.isNull("pkg")){
				//create product if set
				PackageTypePresenter presenter = new PackageTypePresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("pkg");
				item.setPkg(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setPkg(null);
			}
			
			if(!jsonObject.isNull("fgi")){
				//create product if set
				FGIViewPresenter presenter = new FGIViewPresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("fgi");
				item.setFgi(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setFgi(null);
			}
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(InvColumn item){
		try{
			JSONObject json = new JSONObject();

			if(item.getPkg()!=null){
				final PackageTypePresenter presenter = new PackageTypePresenter();
				json.put("pkg", presenter.createJSON(item.getPkg()));
			} else {
				json.put("pkg", JSONObject.NULL);
			}

			if(item.getFgi()!=null){
				final FGIViewPresenter presenter = new FGIViewPresenter();
				json.put("fgi", presenter.createJSON(item.getFgi()));
			} else {
				json.put("fgi", JSONObject.NULL);
			}
			
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public InvColumn getEmptyEntity(){
		InvColumn entity = new InvColumn();
		return entity;
	}
}
